package com.bootcamp.s3_1013.database._1016_jdbc;

import com.mysql.jdbc.Driver;

import java.sql.*;

/**
 * @program: myClassCode
 * @description: my first jdbc demo
 * @author: Keyu Li
 * @create: 2021-10-16 11:06
 **/

public class Demo1_jdbc {
    public static void main(String[] args) throws SQLException {
        // 1.加载驱动
        DriverManager.registerDriver(new Driver());
        // 2.获取连接，直接在代码中赋值称为魔法值
        String url = "jdbc:mysql://localhost:3306/35th_test?useSSL=false";
        String username = "root";
        String password = "123456";
        Connection connection = DriverManager.getConnection(url, username, password);
        // 3.获取statement对象，用以执行sql语句
        Statement statement = connection.createStatement();
        // 4.获取执行结果
//        String sql = "insert into city values(5,'蚌埠',34),(6,'苏州',32)";
//        String sql="delete from t_dd where age=70";
        String sql = "select id,name city,pid from city";
        ResultSet resultSet = statement.executeQuery(sql);
        // 4.2 解析resultset结果
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String city = resultSet.getString("city");
            int pid = resultSet.getInt("pid");
            // 5.展示执行结果
            System.out.println("id:" + id + ", city name:" + city + ", province id:" + pid);
        }
        // 6.关闭连接，释放资源
        statement.close();
        connection.close();

    }
}
